package com.gxa.www.cl.controller;

import com.gxa.www.cl.common.base.RespResult;
import com.gxa.www.cl.domain.entity.Sale;
import com.gxa.www.cl.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sales")
public class SaleController {

    @Autowired
    private SaleService salesService;

    @GetMapping("/show")
    public RespResult showSales() {
        // 获取销售数据
        List<Sale> salesList = salesService.getAllSales();

        // 创建 RespResult 并封装销售数据
        RespResult result = new RespResult();
        result.setCode(200);  // 假设 200 表示成功
        result.setMessage("获取销售数据成功");
        result.setData(salesList);  // 将销售数据放入 RespResult 的 data 字段中

        return result;
    }


    @DeleteMapping("/delete/{id}")
    public RespResult deleteSale(@PathVariable Integer id) {
        try {
            salesService.deleteSale(Long.valueOf(id));
            return new RespResult(200, "删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(500, "删除失败: " + e.getMessage(), null);
        }
    }

}
